#include "PathApi.h"
#include "Shlwapi.h"
#include <string>

bool fileExist(const char* filePath)
{
	bool _fileExist;
#if ! defined (_UNICODE) && ! defined(UNICODE) 
	_fileExist = PathFileExists((LPCTSTR)filePath)? true:false;
#else
	wchar_t wstr[MAX_PATH];
	int wchars_num = MultiByteToWideChar(CP_UTF8, 0, filePath, -1, NULL, 0);
	MultiByteToWideChar(CP_UTF8, 0, filePath, -1, wstr, wchars_num);
	_fileExist = PathFileExists(wstr)? true:false;
#endif
	return _fileExist;
}

bool directoryExist(const char* directoryPath)
{
	bool _directoryExist;
#if ! defined (_UNICODE) && ! defined(UNICODE) 
	_directoryExist = PathFileExists((LPCTSTR)directoryPath) ? true : false;
#else
	wchar_t wstr[MAX_PATH];
	int wchars_num = MultiByteToWideChar(CP_UTF8, 0, directoryPath, -1, NULL, 0);
	MultiByteToWideChar(CP_UTF8, 0, directoryPath, -1, wstr, wchars_num) ? true : false;
	_directoryExist = PathFileExists(wstr) ? true : false;
#endif
	return _directoryExist;
}

bool directoryEmpty(const char* directoryPath)
{
	if (!directoryExist(directoryPath))
		return false;

	bool _directoryExist;
#if ! defined (_UNICODE) && ! defined(UNICODE) 
	_directoryExist = PathIsDirectoryEmpty((LPCTSTR)directoryPath) ? true : false;
#else
	wchar_t wstr[MAX_PATH];
	int wchars_num = MultiByteToWideChar(CP_UTF8, 0, directoryPath, -1, NULL, 0);
	MultiByteToWideChar(CP_UTF8, 0, directoryPath, -1, wstr, wchars_num) ? true : false;
	_directoryExist = PathIsDirectoryEmpty(wstr) ? true : false;
#endif
	return _directoryExist;
}

bool fileName(const char* filePath, char* fileName)
{
#if ! defined (_UNICODE) && ! defined(UNICODE) 
	LPSTR fileNamePtr;
	fileNamePtr = PathFindFileName((LPCTSTR)filePath);
	size_t fileNameLen = strlen(fileNamePtr);
	if (!fileNameLen)
		*fileName = '\0';
	else
		strncpy_s(fileName, fileNameLen + 1, fileNamePtr, fileNameLen );
#else
	LPWSTR fileNamePtr;
	wchar_t wstr[MAX_PATH];
	int wchars_num = MultiByteToWideChar(CP_UTF8, 0, filePath, -1, NULL, 0);
	MultiByteToWideChar(CP_UTF8, 0, filePath, -1, wstr, wchars_num) ? true : false;
	fileNamePtr = PathFindFileName(wstr);

	int nUserNameLenUnicode = lstrlenW(fileNamePtr); // Convert all UNICODE characters
	int nUserNameLen = WideCharToMultiByte(CP_ACP, // ANSI Code Page
		0, // No special handling of unmapped chars
		fileNamePtr, // wide-character string to be converted
		nUserNameLenUnicode,
		NULL, 0, // No output buffer since we are calculating length
		NULL, NULL); // Unrepresented char replacement - Use Default 
	//pszUserName = new char[nUserNameLen]; // nUserNameLen includes the NULL character
	WideCharToMultiByte(CP_ACP, // ANSI Code Page
		0, // No special handling of unmapped chars
		fileNamePtr, // wide-character string to be converted
		nUserNameLenUnicode,
		fileName,
		nUserNameLen,
		NULL, NULL); // Unrepresented char replacement - Use Default
#endif
	return true;
}
